package cn.flant.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Account {
	private boolean isDebug;

	private String emailAddress;
	private String password;
	private boolean needAuth;

	private String pop3Host;
	private int pop3Port = 110;
	private String smtpHost;
	private int smtpPort = 25;

	public boolean isDebug() {
		return isDebug;
	}

	public void setDebug(boolean isDebug) {
		this.isDebug = isDebug;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isNeedAuth() {
		return needAuth;
	}

	public void setNeedAuth(boolean needAuth) {
		this.needAuth = needAuth;
	}

	public String getPop3Host() {
		return pop3Host;
	}

	public void setPop3Host(String pop3Host) {
		this.pop3Host = pop3Host;
	}

	public int getPop3Port() {
		return pop3Port;
	}

	public void setPop3Port(int pop3Port) {
		this.pop3Port = pop3Port;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	public int getSmtpPort() {
		return smtpPort;
	}

	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
	}

	public Authenticator getAuthenticator() {
		return new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(emailAddress, password);
			}
		};
	}

	public Properties getProperties() {
		Properties prop = new Properties();
		prop.put("mail.debug", String.valueOf(isDebug));
		prop.put("mail.debug.auth", String.valueOf(false));
		prop.put("mail.smtp.auth", String.valueOf(needAuth));
		prop.put("mail.from", emailAddress);
		prop.put("mail.user", emailAddress);
		if (smtpHost != null) {
			prop.put("mail.smtp.host", smtpHost);
		}
		prop.put("mail.smtp.port", String.valueOf(smtpPort));
		if (pop3Host != null) {
			prop.put("mail.pop3.host", pop3Host);
		}
		prop.put("mail.pop3.port", String.valueOf(pop3Port));
		prop.put("mail.store.protocol", "pop3");
		return prop;
	}
}
